
cd 'D:\data_replication'

clear
clc

% Import Income data by i_k and market
%==========================================================================

load('estimation/4_demand_estimation/4_cobb_douglas_weights/Y_i_summary.mat');
Y_i_summary = array2table(Y_i_summary);
Y_i_summary.Properties.VariableNames(1:4) = {'i_k', 'year', 'quarter' 'declarant'};  


% Crosswalk to k (4092 categories)
%==========================================================================

crosswalk_k = readtable('estimation/5_supply_side/k_to_i_k.csv');
Y_i_summary_k = join(Y_i_summary, crosswalk_k, 'Keys', 'i_k');


% Crosswalk to k original (4221 categories)
%==========================================================================

crosswalk_id = readtable('estimation/4_demand_estimation/pc8plus_i_k.csv');
crosswalk_id = crosswalk_id(:, 2:3);
crosswalk_id.Properties.VariableNames = {'id', 'k'};
crosswalk_id(~crosswalk_id.k ~= 0,:) = [];
Y_i_summary_id = join(Y_i_summary_k, crosswalk_id, 'Keys', 'k');


% Crosswalk to pc8plus and keep only 8-digit and synthetic ones
%==========================================================================

crosswalk_pc8plus = readtable('estimation/4_demand_estimation/4_cobb_douglas_weights/id_to_pc8plus.csv');
Y_i_summary_pc8plus = join(Y_i_summary_id, crosswalk_pc8plus, 'Keys', 'id');
Y_i_summary_pc8plus(~Y_i_summary_pc8plus.non_pc8plus ~= 1,:) = [];
Y_i_summary_pc8plus = Y_i_summary_pc8plus(:, 2:108);


% Format and save
%==========================================================================

Y_i_summary_pc8plus = [Y_i_summary_pc8plus(:, 106), Y_i_summary_pc8plus(:, 1:103)];    

data_save = Y_i_summary_pc8plus(:, 1:4);
writetable(data_save, 'estimation/4_demand_estimation/4_cobb_douglas_weights/data_save.csv', 'Delimiter',',','QuoteStrings',true);

save('estimation/4_demand_estimation/4_cobb_douglas_weights/Y_i_summary_pc8plus.mat', 'Y_i_summary_pc8plus');



